package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.qf.captcha.CaptchaCodeManager;
import com.qf.pojo.DtsAdmin;
import com.qf.service.PermissionService;
import com.qf.service.RoleService;
import com.qf.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.PseudoColumnUsage;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/auth")
@CrossOrigin("http://localhost")
public class AdminAuthController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    ApplicationContext context;
    private HashMap<String, String> systemPermissionsMap = null;


    @RequestMapping("/info")
    public Object info(){

        Subject subject = SecurityUtils.getSubject();

       // System.out.println("你好啊啊啊啊啊"+subject.hasRole("超级管理员"));
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

        Map<String,Object> map = new HashMap<>();

        map.put("name",admin.getUsername());
        map.put("avatar",admin.getAvatar());

        map.put("roles",roleService.queryByIds(admin.getRoleIds()));
        map.put("perms",permissionService.queryByIds(admin.getRoleIds()));

        return ResponseUtil.ok(map);

    }



    /**
     * 获取验证码
     */
    @RequestMapping("/captchaImage")
    public Object captchaImage(HttpServletResponse response) throws IOException {
        String code = VerifyCodeUtils.generateVerifyCode(1);
        //生成 uuid， 验证码 唯一的标识
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        //在缓存区 存储验证码
        CaptchaCodeManager.addToCache(uuid, code, 10);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //输出图片
        VerifyCodeUtils.outputImage(111, 37,outputStream  ,code );


        Map<String, Object> data  = new HashMap<>();
        data.put("img", Base64.encode(outputStream.toByteArray()));
        data.put("uuid", uuid);


        //errno, errmsg, data
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/login")
    public Object login(@RequestBody  Map<String,String> map){

        String username = map.get("username");
        String password = map.get("password");
        String uuid = map.get("uuid");
        String code = map.get("code");

      if(StringUtils.isEmpty(username) || StringUtils.isEmpty(uuid) || StringUtils.isEmpty(code) ||
      StringUtils.isEmpty(password)){
          return ResponseUtil.badArgument();
      }

      //校验验证码
        String cacheCaptcha = CaptchaCodeManager.getCachedCaptcha(uuid);
      if(cacheCaptcha==null){
          System.out.println("系统错误");
      }
      if(!code.equalsIgnoreCase(cacheCaptcha)){
          return AdminResponseUtil.fail(AdminResponseCode.AUTH_CAPTCHA_ERROR);
      }
      //创建shiro的subject对象
        Subject subject = SecurityUtils.getSubject();

            subject.login(new UsernamePasswordToken(username,password));

        JSONObject.toJSONString(subject.getSession().getId());

        return ResponseUtil.ok(subject.getSession().getId());





    }













}
